package com.ruoyi.core.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.core.domain.bo.ConfigBo;
import com.ruoyi.core.domain.vo.ConfigVo;
import com.ruoyi.core.domain.Config;
import com.ruoyi.core.mapper.ConfigMapper;
import com.ruoyi.core.service.IConfigService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 身份标识Service业务层处理
 *
 * @author xie
 * @date 2022-08-26
 */
@RequiredArgsConstructor
@Service
public class ConfigServiceImpl implements IConfigService {

    private final ConfigMapper baseMapper;

    /**
     * 查询身份标识
     */
    @Override
    public ConfigVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询身份标识列表
     */
    @Override
    public TableDataInfo<ConfigVo> queryPageList(ConfigBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Config> lqw = buildQueryWrapper(bo);
        Page<ConfigVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询身份标识列表
     */
    @Override
    public List<ConfigVo> queryList(ConfigBo bo) {
        LambdaQueryWrapper<Config> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Config> buildQueryWrapper(ConfigBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Config> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getWid() != null, Config::getWid, bo.getWid());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), Config::getType, bo.getType());
        lqw.eq(StringUtils.isNotBlank(bo.getAppid()), Config::getAppid, bo.getAppid());
        lqw.eq(StringUtils.isNotBlank(bo.getSecret()), Config::getSecret, bo.getSecret());
        lqw.eq(StringUtils.isNotBlank(bo.getToken()), Config::getToken, bo.getToken());
        lqw.eq(StringUtils.isNotBlank(bo.getAeskey()), Config::getAeskey, bo.getAeskey());
        lqw.eq(StringUtils.isNotBlank(bo.getIsPay()), Config::getIsPay, bo.getIsPay());
        lqw.eq(StringUtils.isNotBlank(bo.getPartner()), Config::getPartner, bo.getPartner());
        lqw.eq(StringUtils.isNotBlank(bo.getPartnerKey()), Config::getPartnerKey, bo.getPartnerKey());
        lqw.eq(StringUtils.isNotBlank(bo.getIsused()), Config::getIsused, bo.getIsused());
        lqw.eq(StringUtils.isNotBlank(bo.getRedirectUrl()), Config::getRedirectUrl, bo.getRedirectUrl());
        lqw.eq(StringUtils.isNotBlank(bo.getApivkey()), Config::getApivkey, bo.getApivkey());
        lqw.eq(StringUtils.isNotBlank(bo.getIkeyPath()), Config::getIkeyPath, bo.getIkeyPath());
        lqw.eq(StringUtils.isNotBlank(bo.getKeyPath()), Config::getKeyPath, bo.getKeyPath());
        lqw.eq(StringUtils.isNotBlank(bo.getIprivateKeyPath()), Config::getIprivateKeyPath, bo.getIprivateKeyPath());
        lqw.eq(StringUtils.isNotBlank(bo.getPrivateKeyPath()), Config::getPrivateKeyPath, bo.getPrivateKeyPath());
        lqw.eq(StringUtils.isNotBlank(bo.getIprivateCertPath()), Config::getIprivateCertPath, bo.getIprivateCertPath());
        lqw.eq(StringUtils.isNotBlank(bo.getPrivateCertPath()), Config::getPrivateCertPath, bo.getPrivateCertPath());
        lqw.eq(StringUtils.isNotBlank(bo.getCertSerialNo()), Config::getCertSerialNo, bo.getCertSerialNo());
        return lqw;
    }

    /**
     * 新增身份标识
     */
    @Override
    public Boolean insertByBo(ConfigBo bo) {
        Config add = BeanUtil.toBean(bo, Config.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改身份标识
     */
    @Override
    public Boolean updateByBo(ConfigBo bo) {
        Config update = BeanUtil.toBean(bo, Config.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Config entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除身份标识
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public List<ConfigVo> listWxConfig() {
        LambdaQueryWrapper<Config> query = Wrappers.lambdaQuery();
        query.eq(Config::getType,"小程序")
            .eq(Config::getIsused,"1");
        return baseMapper.selectVoList(query);
    }
}
